//
//  ViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/6.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

//1:去除了NS前缀
//2:采用句点表示法
//3:取消了预编译指令
//print 效率比NSLog高
// var定义变量，let定义常量
//添加文档注释 cmd + option + /

import UIKit



class ViewController: UIViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        demo6(10, 20)
        demo9(name: "12", age: 22)
//        let myBtn = UIButton(type: .custom)
//        myBtn.backgroundColor = UIColor.red
//        myBtn.frame = CGRect(x: 100, y: 200, width: 100, height: 30)
//        myBtn.addTarget(self, action: #selector(nextPage), for: .touchUpInside)
//        view.addSubview(myBtn)
//        
        // Do any additional setup after loading the view, typically from a nib.
    }

  
    //点击事件
    func clickMe(btn: UIButton)
    {
        //#function打印函数名
        print(#function)
        print("ni hao")
        print(btn)
        
        //跳转界面
//        let vc = SecondViewController()
//        
//        present(vc, animated: true) {
//            
//        }
    }
    

    func demo()
    {
        //热键option+click
        var x = 20
        x = 30
        print(x)
        let y = 39.09
        print(y)
        
        let v = UIView()
        print(v)
    }
    //MARK: - Swift中不存在基本数据类型，都是结构体
    func demo1()
    {
        //任何不同类型的数据之间，不允许直接运算
        let x1 = 10
        let y1 = 98.2
        print(x1 + Int(y1))
        print(Double(x1) + y1)
    }
    //MARK: - 任何不同类型的数据之间，不允许直接运算
    func demo2()
    {
        
        //Swift中不存在基本数据类型，都是结构体
        let x1: Double = 10
        let y1 = 98.2
        print(x1 + y1)
        
        //不可变比可变更安全，程序中尽量用不可变的
        let x = 20
        let y = 309
        print(x + y)
    }

    //MARK:可选项
    func demo3()
    {
        let x = 100
        //可选值

        let y: Optional = 90
        let z: Int? = 98
        print(x)
        print(y)
        print(z)
        // !强行解包 从可选值中强行获取对应的非空值，如果是nil，就会崩溃
        print(z! + y!)
        
        //let的可选值默认没有初始化，没有可选值
        //var的可选值默认为nil
        let a: Int?
        a = 8765
        print(a)
        
        var b: Int?
        print(b)
    }
    
    //MARK: - 视图生命周期
    func demo4()
    {
        //MARK: 创建视图
        let myView = UIView(frame: CGRect(x: 10, y: 20, width: 200, height: 200))
        //修改view的属性，没有修改view的指针
        myView.backgroundColor = UIColor.green
        myView.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.1333333333, alpha: 1) //TODO: 设置颜色
        view.addSubview(myView)
        
        //MARK: 创建按钮
        let btn = UIButton(type: .contactAdd)
        myView.addSubview(btn)
        //添加事件
        btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
        
        //MARK: 创建UIImageView
        let imageView = UIImageView(image: #imageLiteral(resourceName: "melon")) //FIXME: 设置图片
    
        //let imageView1 = UIImageView(image: #imageLiteral(resourceName: "melon"))
        
        imageView.center = view.center
        view.addSubview(imageView)
    }
    
    //MARK: - 条件句
    func demo5()
    {
        let x = 20
        if x > 20
        {
            
            print("打了")
        }
        else
        {
            print("小了")
        }
        
        //三目运算符
        let y = 12
        y > 92 ? print("好棒") : print("衰")
        y > 92 ? print("好棒") : ()
    }
    //MARK: - ??的使用
    func demo6(_ x: Int?,_ y: Int?)
    {
        let m: Int? = 100
        let n: Int? = 200
        print(m! + n!)
        if x != nil && y != nil
        {
            print(x! + y!)
        }
        else
        {
            print("x 或者 y 为nil")
        }
        
        //如果有值就取值，没值用 ？？后面的值代替
        print((x ?? 10) + (y ?? 10))
        
        
        let name: String? = nil
        print((name ?? "") + "你好")
        
        let day: String? = "小雨"
        print((day ?? "") + "天气")
        print(day ?? "" + "天气")
    }
    
    //MARK: if let / if var连用判断值是否为空，不是单纯的if
    func demo7()
    {
        let name: String? = "贝贝"
        let age: Int? = 3
        // ! 解包
        if name != nil && age != nil
        {
            print(name! + String(age!))
        }
        
        print((name ?? "") + String(age ?? 0))
        
        //if let组合，进入分支，{}内变量一定有值 不需要解包
        if let name1 = name,let age1 = age
        {
            print(name1 + String(age1))
        }
        else
        {
            print("有nil值")
        }
        
        if var name1 = name,var age1 = age
        {
            name1 = "great good nice ！"
            age1 = 20
            print(name1 + String(age1))
        }
        else
        {
            print("有nil值")
        }
    }
    
    //MARK: guard let 和 if let 正好相反
    func demo8()
    {
        let name: String? = "贝贝"
        let age: Int? = 3
        //guard let 守护一定有值，没有值直接返回
        guard let name1 = name,let age1 = age else
        {
            print("name 或者 age 存在空值")
            return
        }
        //代码执行至此，name1和age1一定有值
        
        print(name1 + String(age1))
    }
    
    func demo9(name: String?,age: Int?)
    {
        if let name = name,let age = age
        {
            //非空进入
            print(name + String(age))
        }
//        else
//        {
//            print("有空值nil")
//        }
        
        guard let name = name,let age = age else
        {
            //空值进入
            print("nil值进入")
            return
        }
        print(name + String(age))
    }
    
    //MARK: - for循环 
    //for-in 
    //0..<10 [0,10)
    //0...10 [0,10]
    func demo10()
    {
        var sum = 0
        
        for i in 0...5
        {
            sum += i
        }
        print("sum=",sum)
        
        var total = 0
        
        for i in 0..<5
        {
            total += i
        }
        print("total=",total)
        print("total= \(total)")
    }
    
    func demo11(name: String?,age: Int?)
    {
        if let name = name,let age = age
        {
            //非空进入
            print(name + String(age))
        }
        
        print((name ?? "") + String(age ?? 0))
        
        print("-----")
        guard let name1 = name,let age1 = age else
        {
            //空值进入
            print("nil值进入")
            //减少一个层次
            return
        }

        print(name1 + String(age1))
        
        
    }
    
    
    func demo12()
    {
        let a = 100
        var b = 200
        b = 203
        print(a)
        print(b)
        print("a=\(a)")
        print("b=\(b)")
        
        
    }
    //MARK: - switch使用
    func demo13(number: String)
    {
  
        /*
         switch可以针对任意数据类型进行分支，不再局限于整数
         switch一般不需要break
         switch如果多值，使用，号分割
         所有的分支至少需要一条指令，如果什么都不做，使用break
         */
        
        switch number
        {
        case "12":
            print("nihao")
        case "10":
            print("124")
        case "11":
            break
        default:
            print("hate")
        }
    }
    
    func demo14()
    {
        let point = CGPoint(x: 10, y: 10)
        
        switch point
        {
        case let p where p.x==0 && p.y==0:
            print("原点")
        case let p where p.x==0:
            print("Y轴")
        case let p where p.y==0:
            print("X轴")
        case let p where abs(p.x) == abs(p.y):
            print("对角线")
        default:
            print("其它")
        }
    }
    
    
        
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



